{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot multi-class SGD on the iris dataset\n\n\nPlot decision surface of multi-class SGD on iris dataset.\nThe hyperplanes corresponding to the three one-versus-all (OVA) classifiers\nare represented by the dashed lines.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\nfrom sklearn.linear_model import SGDClassifier\n\n# import some data to play with\niris = datasets.load_iris()\n\n# we only take the first two features. We could\n# avoid this ugly slicing by using a two-dim dataset\nX = iris.data[:, :2]\ny = iris.target\ncolors = \"bry\"\n\n# shuffle\nidx = np.arange(X.shape[0])\nnp.random.seed(13)\nnp.random.shuffle(idx)\nX = X[idx]\ny = y[idx]\n\n# standardize\nmean = X.mean(axis=0)\nstd = X.std(axis=0)\nX = (X - mean) / std\n\nh = .02  # step size in the mesh\n\nclf = SGDClassifier(alpha=0.001, max_iter=100).fit(X, y)\n\n# create a mesh to plot in\nx_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1\ny_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1\nxx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n                     np.arange(y_min, y_max, h))\n\n# Plot the decision boundary. For that, we will assign a color to each\n# point in the mesh [x_min, x_max]x[y_min, y_max].\nZ = clf.predict(np.c_[xx.ravel(), yy.ravel()])\n# Put the result into a color plot\nZ = Z.reshape(xx.shape)\ncs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)\nplt.axis('tight')\n\n# Plot also the training points\nfor i, color in zip(clf.classes_, colors):\n    idx = np.where(y == i)\n    plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],\n                cmap=plt.cm.Paired, edgecolor='black', s=20)\nplt.title(\"Decision surface of multi-class SGD\")\nplt.axis('tight')\n\n# Plot the three one-against-all classifiers\nxmin, xmax = plt.xlim()\nymin, ymax = plt.ylim()\ncoef = clf.coef_\nintercept = clf.intercept_\n\n\ndef plot_hyperplane(c, color):\n    def line(x0):\n        return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]\n\n    plt.plot([xmin, xmax], [line(xmin), line(xmax)],\n             ls=\"--\", color=color)\n\n\nfor i, color in zip(clf.classes_, colors):\n    plot_hyperplane(i, color)\nplt.legend()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}